var ObjectID = require('mongodb').ObjectID;
var pool = require('./db').getPool();

function Comment(comment) {
    this.comment = comment;
}

module.exports = Comment;

Comment.prototype.save = function (_id, callback) {
   var comment = this.comment;
   pool.acquire(function (err, mongodb) {
       if(err){
           return callback(err);
       }
       mongodb.collection('posts', function (err, collection) {
           if (err){
               pool.release(mongodb);
               return callback(err);
           }
           collection.update({
               "_id": new ObjectID(_id)
           }, {
               $push:{"comments": comment}
           }, function (err) {
               pool.release(mongodb);
               if (err){
                   return callback(err);
               }
               callback(null);
           });
       });
   });
};